'use strict';

angular.module('snPaas_om')
	.controller('WorkorderCtrl',['$scope','baseUrl','workorderStore','ngDialog','SpSlider',function($scope,baseUrl,workorderStore,ngDialog,SpSlider){

		$scope.baseUrl = baseUrl.static;
		$scope.types = [
			{
				name: '全部',
				id: 0
			},
			{
				name: '未读',
				id: 1
			},
			{
				name: '处理中',
				id: 2
			},
			{
				name: '已关闭',
				id: 3
			},
		];
		$scope.typeNow = 0;
		$scope.pageNow = 1;

		var loadList = function(page){
			workorderStore.getList($scope.typeNow,page).then(function(data){
				$scope.workorderList = data.workorderList;
				$scope.page = data.page;
				$scope.pageList = [];
				for(var i = 1;i<=data.page;i++){
					$scope.pageList.push(i);
				}
				$scope.pageNow = data.pageNow;
			});
		};

		$scope.$watch('typeNow',function(n,o){
			if(n!= undefined){
				loadList($scope.pageNow);
			}
		});
		$scope.detail = function(wo){
			wo.readState = 0;
			ngDialog.open({
				template: baseUrl.static+'template/views/workOrderDetail.html',
				controller: 'WorkOrderDetailCtrl',
				width:926,
				showClose:false,
				data:wo
			});

		};
		$scope.closeWo = function(wo){
			workorderStore.close(wo.id).then(function(){
				SpSlider.slideIn({
					title:'操作成功',
					content:'工单 '+wo.orderName+' 已关闭',
					type:'noti'
				});

				loadList($scope.pageNow);
			});
		};
		loadList($scope.pageNow);
	}]);